package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.CommodityRepertory;
import com.ruoyi.recycle.domain.CommoditySizeConfig;
import com.ruoyi.recycle.domain.HomePageSlideshow;
import com.ruoyi.recycle.domain.IntegralCommodity;
import com.ruoyi.recycle.dto.IntegralCommodityDTO;
import com.ruoyi.recycle.dto.IntegralCommodityVO;
import com.ruoyi.recycle.service.IIntegralCommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 积分商品Controller
 *
 * @author Wubk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/commodity")
@Api(tags = "积分商品")
public class IntegralCommodityController extends BaseController {
    @Autowired
    private IIntegralCommodityService integralCommodityService;

    /**
     * 查询积分商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IntegralCommodity integralCommodity) {
        startPage();
        List<IntegralCommodity> list = integralCommodityService.selectIntegralCommodityList(integralCommodity);
        return getDataTable(list);
    }

    /**
     * 分页查询积分商品列表-
     */
    @PostMapping("/commodityPageQuery")
    @ApiOperation("分页查询积分商品列表")
    public IPage<IntegralCommodity> commodityPageQuery(@RequestBody IntegralCommodityDTO queryDTO) throws Exception{
        final IntegralCommodity factory = new IntegralCommodity();
        BeanUtils.copyProperties(queryDTO, factory);
        final LambdaQueryWrapper<IntegralCommodity> wrapper = Wrappers.lambdaQuery(factory);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(IntegralCommodity::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(IntegralCommodity::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));
        }
        if (queryDTO.getCommodityName() != null) {
            wrapper.like(IntegralCommodity::getCommodityName,queryDTO.getCommodityName());
        }
        wrapper.orderByDesc(IntegralCommodity::getCreatedTime);
        return integralCommodityService.page(new Page<IntegralCommodity>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }


    /**
     * 导出积分商品列表
     */
    @RequiresPermissions("recycle:commodity:export")
    @Log(title = "积分商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出积分商品列表")
    public void export(HttpServletResponse response, IntegralCommodity integralCommodity) {
        List<IntegralCommodity> list = integralCommodityService.selectIntegralCommodityList(integralCommodity);
        ExcelUtil<IntegralCommodity> util = new ExcelUtil<IntegralCommodity>(IntegralCommodity.class);
        util.exportExcel(response, list, "积分商品数据");
    }

    /**
     * 获取积分商品详细信息
     */
    @RequiresPermissions("recycle:commodity:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取积分商品详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(integralCommodityService.selectIntegralCommodityById(id));
    }

    /**
     * 新增积分商品
     */
    @RequiresPermissions("recycle:commodity:add")
    @Log(title = "积分商品", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增积分商品")
    public AjaxResult add(@RequestBody IntegralCommodityVO integralCommodity) {
        return toAjax(integralCommodityService.insertIntegralCommodity(integralCommodity));
    }

    /**
     * 修改积分商品
     */
    @RequiresPermissions("recycle:commodity:edit")
    @Log(title = "积分商品", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改积分商品")
    public AjaxResult edit(@RequestBody IntegralCommodityVO integralCommodity) {
        return toAjax(integralCommodityService.updateIntegralCommodity(integralCommodity));
    }

    /**
     * 删除积分商品
     */
    @RequiresPermissions("recycle:commodity:remove")
    @Log(title = "积分商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除积分商品")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(integralCommodityService.deleteIntegralCommodityByIds(ids));
    }

    /**
     * 插入积分商品规格尺寸
     */
    @PostMapping("/insertCommoditySizeConfig")
    @ApiOperation("插入积分商品规格尺寸")
    public AjaxResult insertCommoditySizeConfig(@RequestBody CommoditySizeConfig commoditySizeConfig) {
        return integralCommodityService.insertCommoditySizeConfig(commoditySizeConfig);
    }

    /**
     * 删除积分商品规格尺寸
     */
    @PostMapping("/deleteCommoditySizeConfig")
    @ApiOperation("删除积分商品规格尺寸")
    public AjaxResult deleteCommoditySizeConfig(@RequestBody CommoditySizeConfig commoditySizeConfig) {
        return integralCommodityService.deleteCommoditySizeConfig(commoditySizeConfig);
    }

    /**
     * 获取积分商品规格尺寸
     */
    @PostMapping("/selectCommoditySizeConfig")
    @ApiOperation("获取积分商品规格尺寸")
    public AjaxResult deleteCommoditySizeConfig(@RequestParam Integer commodityId) {
        return integralCommodityService.selectCommoditySizeConfig(commodityId);
    }

    /**
     * 更新积分商品采购与退货库存记录
     */
    @PostMapping("/addCommodityRepertory")
    @ApiOperation("更新积分商品采购与退货库存记录")
    public AjaxResult addCommodityRepertory(@RequestBody List<CommodityRepertory> commodityRepertoryList) {
        return integralCommodityService.addCommodityRepertory(commodityRepertoryList);
    }

    /**
     * 获取积分商品采购与退货库存记录
     */
    @PostMapping("/getCommodityRepertory")
    @ApiOperation("获取积分商品采购与退货库存记录")
    public AjaxResult getCommodityRepertory(@RequestBody CommodityRepertory commodityRepertory) {
        return integralCommodityService.getCommodityRepertory(commodityRepertory);
    }




}
